package com.wl.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wl.dao.Delivery_07Mapper;
import com.wl.dao.LoginfoMapper;
import com.wl.dao.Warehouse_06Mapper;
import com.wl.entity.Delivery_07;
import com.wl.entity.Loginfo;
import com.wl.entity.Warehouse_06;
import com.wl.service.Warehouse_06Service;
import com.wl.util.PageData;

/**
 * 业务层实现类
 * 
 * @author HCH
 *
 */
@Service
public class Warehouse_06ServiceImpl implements Warehouse_06Service {

	// 注入一个Warehouse_06Mapper
	@Autowired
	private Warehouse_06Mapper warehouseMapper;

	// 注入一个Delivery_07Mapper
	@Autowired
	private Delivery_07Mapper deliveryMapper;

	// 注入loginfomapper
	@Autowired
	private LoginfoMapper logmapper;

	/**
	 * 动态sql语句添加业务信息
	 */
	@Override
	public boolean insertSelective(Warehouse_06 record) {
		// TODO Auto-generated method stub
		int n = warehouseMapper.insertSelective(record);
		if (n > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据当前处理人编号查找尚未处理的业务并分页
	 */
	@Override
	public PageData<Warehouse_06> selectWarehouseByAppno(int page, int limit, String appno) {
		// TODO Auto-generated method stub
		// 设置起始位置
		int start = 0;
		if (page >= 1) {
			start = (page - 1) * limit;
		}
		// 查询总页数
		int totalCount = warehouseMapper.selectWarehouseByAppnoCount(appno);
		// 分页的数据
		List<Warehouse_06> data = warehouseMapper.selectWarehouseByAppno(start, limit, appno);
		// 构造方法：赋值内容为 数据、总记录数、limit、page
		PageData<Warehouse_06> pageData = new PageData<Warehouse_06>(data, totalCount, limit, page);
		// code、Msg，前端layui所需要的参数
		pageData.setCode(0);
		pageData.setMsg("");
		return pageData;
	}

	/**
	 * 根据flowid动态修改
	 */
	@Override
	public boolean updateByPrimaryKeySelective(Warehouse_06 record) {
		// TODO Auto-generated method stub
		int n = warehouseMapper.updateByPrimaryKeySelective(record);
		if (n > 0) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 处理当前入库业务，并转发给下一领料业务 propagation = Propagation.REQUIRED 设置事务的传播特性. isolation
	 * 设置事务的隔离级别,READ_COMMITTED 避免脏读
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public boolean warehouseAndDelivery(Warehouse_06 warehouse, Delivery_07 delivery, Loginfo log) {
		// TODO Auto-generated method stub
		int n = warehouseMapper.updateByPrimaryKeySelective(warehouse);
		int m = deliveryMapper.insertSelective(delivery);
		int o = logmapper.insert(log);
		if (n > 0 && m > 0 && o > 0) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 分页+动态查询业务
	 */
	@Override
	public PageData<Warehouse_06> selectWarehouseByPage(int page, int limit, String flowid, String appno,
			String appresult, String starttime,String endtime, Integer appstatus, String applyno) {
		// TODO Auto-generated method stub
		// 设置起始位置
		int start = 0;
		if (page >= 1) {
			start = (page - 1) * limit;
		}
		// 查询总页数
		int totalCount = warehouseMapper.selectWarehouseByPageCount(flowid, appno, appresult, starttime, endtime,appstatus,
				applyno);
		// 分页的数据
		List<Warehouse_06> data = warehouseMapper.selectWarehouseByPage(start, limit, flowid, appno, appresult,
				 starttime, endtime, appstatus, applyno);
		// 构造方法：赋值内容为 数据、总记录数、limit、page
		PageData<Warehouse_06> pageData = new PageData<Warehouse_06>(data, totalCount, limit, page);
		// code、Msg，前端layui所需要的参数
		pageData.setCode(0);
		pageData.setMsg("");
		return pageData;
	}
	
	/**
	 * 根据flowid查找业务记录
	 */
	@Override
	public Warehouse_06 selectByFlowid(String flowid) {
		// TODO Auto-generated method stub
		Warehouse_06 warehouse=warehouseMapper.selectByFlowid(flowid);
		return warehouse;
	}
}
